<?php
/**
 * Created by PhpStorm.
 * User: 38339
 * Date: 2021-05-26
 * Time: 14:58
 */

namespace app\common\business\mysql;
use app\common\model\mysql\CommonAdmin as models;


class CommonAdmin
{

    public static function checkToken($token){
        $admin = models::getOne(['token'=>$token]);
        if(empty($admin)){
            jsonFail('请重新登录',7);
        }
        if($admin['status'] != 1){
            jsonFail('该帐号已禁用',7);
        }
        return $admin;
    }

    public static function login($load){
        $token = api_token($load['id']);
        models::where(['id'=>$load['id']])->update(
            [
                'token'=>$token,
                'token_flush_at'=>date('Y-m-d H:i:s')
            ]);
        //写入日志
        CommonLog::insertLog($load['id'],'/CommonAdmin/login',$load,'',4);
        return $token;
    }

    /*
     * 获取单条数据
     */
    public static function getOne($where){
        return models::getOne($where);
    }

    /*
     * 获取数据
     */
   public static function getList($where,$limit_start,$page_size){
       return models::getList($where,$limit_start,$page_size);
   }

   /*
    * 获取数据条数
    */
   public static function getCount($where){
       return getAllPage(models::getCount($where));
   }

   /*
    * 新增数据
    */
   public static function insert($receiveData,$admin){
       $data = [
           'username' => $receiveData['username'],
           'city_config_arr' => json_encode($receiveData['city_config_arr']),
           'status' => $receiveData['status'] == 1 ? 1 : 2,
           'role_id' => $receiveData['role_id'],
           'city_config_id' => end($receiveData['city_config_arr']),
           'remark' => $receiveData['remark'],
           'mobile' => $receiveData['mobile'],
           'password' => pwdMd5($receiveData['password']),
           'create_time' => date('Y-m-d H:i:s')
       ];
       models::insert($data);
       //写入日志
       CommonLog::insertLog($admin['id'],'/commonAdmin/insert','',$data,1);
       return true;
   }

   /*
    * 修改数据
    */
   public static function edit($receiveData,$admin){
       $data = [
           'username' => $receiveData['username'],
           'city_config_arr' => json_encode($receiveData['city_config_arr']),
           'status' => $receiveData['status'] == 1 ? 1 : 2,
           'role_id' => $receiveData['role_id'],
           'city_config_id' => end($receiveData['city_config_arr']),
           'remark' => $receiveData['remark'],
           'mobile' => $receiveData['mobile'],
       ];
       if(!empty($receiveData['password'])){
           $data['password'] = pwdMd5($receiveData['password']);
       }
       models::where(['id'=>$receiveData['id']])->update($data);
       //写入日志
       CommonLog::insertLog($admin['id'],'/commonAdmin/edit','',$data,2);
       return true;
   }

   /*
    * 更改数据状态
    */
   public static function update($receiveData){

   }

    /*
     * 删除数据
     */
    public static function delete($receiveData,$admin,$load){
        models::where(['id'=>$receiveData['id']])->delete();
        //写入日志
        CommonLog::insertLog($admin['id'],'/commonAdmin/delete',$load,'',3);
        return true;
    }

}